package com.me.components.map {
	import com.me.components.DragComponent;
	import com.me.components.MapResource;
	import com.me.components.fix.MapResourceLayer;
	import com.me.components.fix.MapResourceProperty;
	import com.me.components.pop.MapLayerOption;
	import com.me.obj.Building;

	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.events.CloseEvent;
	import mx.events.DragEvent;
	import mx.events.FlexEvent;

	import spark.components.Group;
	import spark.components.TitleWindow;
	import spark.core.SpriteVisualElement;

	/**
	 *
	 * @author yw
	 *
	 * @Date 2011-9-16 上午09:35:45
	 */
	public class MapScene extends TitleWindow {
		private static var _instance:MapScene;

		private var _contentHeight:int;

		private var _building:BuildingLayer;

		private var _surface:SceneLayer;

		private var _grid:GridLayer;

		public static function getInstance():MapScene {
			if (_instance == null) {
				_instance = new MapScene(new SingletonEnforce());
			}
			return _instance;
		}

		public function MapScene(enforce:SingletonEnforce) {
			if (enforce == null) {
				throw new Error("MapEditorComponent can not be instance");
			}
			title = "地图";
			creationPolicy = "all";
			addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
			addEventListener(CloseEvent.CLOSE, closeHandler);
			addEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
			addEventListener(MouseEvent.MOUSE_UP, addSprite);
		}

		private function creationCompleteHandler(e:FlexEvent):void {
			height = _contentHeight + titleDisplay.height;

			_surface = new SceneLayer(width, _contentHeight);
			addElement(_surface);

			_building = new BuildingLayer(width, _contentHeight);
			addElement(_building);

			_grid = new GridLayer(width, _contentHeight);
			addElement(_grid);

			removeEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
		}

		private function addSprite(e:MouseEvent):void {
			if (DragComponent.getInstance().isDrag) {
				MapLayerOption.getInstance().res = DragComponent.getInstance().resource;
				FlexGlobals.topLevelApplication.popLayer.addElement(MapLayerOption.getInstance());
				MapLayerOption.getInstance().recoordinate();
			}
		}

		private function closeHandler(e:CloseEvent):void {
			if (e.detail == Alert.YES) {
				MapResourceLayer.getInstance().clear();
				MapResourceProperty.getInstance().selectedResource = null;
				_grid.clear();
				_building.clear();
				_surface.clear();
				(parent as Group).removeElement(this);
				return;
			}
			Alert.show("是否确认放弃编辑此地图?.", "提示", Alert.YES | Alert.NO, null, closeHandler);
		}

		private function startDragHandler(e:MouseEvent):void {
//			if (e.localY < titleDisplay.measuredHeight) {
				stage.addEventListener(MouseEvent.MOUSE_UP, stopDrageHandler);
//				startDrag(false, new Rectangle(systemManager.screen.x, systemManager.screen.y, systemManager.screen.width - width, systemManager.screen.height - height));
				startDrag(false);
//			}
		}

		private function stopDrageHandler(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, stopDrageHandler);
			stopDrag();
		}

		public function addSurface(res:MapResource):void {
			var d:Building = new Building(Building.TYPE_BUILDING, res.fileName, Building.LAYER_SURFACE);
			res.attatchment = d;
			if (_surface.numChildren > 0) {
				MapResourceLayer.getInstance().surfaceChanged((_surface.removeChildAt(0) as MapResource).fileName);
			}
			_surface.addChild(res);
			MapResourceLayer.getInstance().addResource(d);
		}

		public function selectBuilding(building:String):void {
			for (var i:int = 0; i < _building.numChildren; i++) {
				var res:MapResource = _building.getChildAt(i) as MapResource;
				res.selected = res.fileName == building;
			}
		}

		public function addBuilding(res:MapResource):void {
//			if (!_building.containsRes(res.fileName)) {
			var d:Building = new Building(Building.TYPE_BUILDING, res.fileName, Building.LAYER_BUILDING);
			res.attatchment = d;
			MapResourceLayer.getInstance().addResource(d);
			_building.addChild(res);
//			}
		}

		public function set contentHeight(value:int):void {
			_contentHeight = value;
		}

		public function get grid():GridLayer {
			return _grid;
		}
	}
}

class SingletonEnforce {

}